/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.delivery.in;

import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.business.vo.WmsInReceiptPage;
import org.jeecg.modules.flow.callback.delivery.util.CommonDeliveryTaskUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库单保存编辑回调前bean
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-09-26
 */
@Component
public class InBoundReceiptDBRKSaveBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptService wmsInReceiptService;

    @Autowired
    private CommonDeliveryTaskUtil commonDeliveryTaskUtil;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 转class.自定义class,data对应的class
        baseFlowDto.translateClass(WmsInReceiptPage.class);
        WmsInReceiptPage data = (WmsInReceiptPage) baseFlowDto.getData();
        WmsInReceipt wmsInReceipt = new WmsInReceipt();
        BeanUtils.copyProperties(data, wmsInReceipt);
        //commonDeliveryTaskUtil.checkInReceiptMaterialWarehouseArea(wmsInReceipt.getDocumentType(),data.getWmsInReceiptMaterialList());
        commonDeliveryTaskUtil.checkExistInTransitRepertory(wmsInReceipt.getWmsInReceiptTaskId());
        if (StrUtil.isNotBlank(data.getId())){
            wmsInReceiptService.updateMain(wmsInReceipt, data.getWmsInReceiptMaterialList());
            baseFlowDto.setResult(Result.OK("编辑成功"));
        }else {
            String s = wmsInReceiptService.saveMain(wmsInReceipt, data.getWmsInReceiptMaterialList());
            baseFlowDto.setInfoId(s);
            baseFlowDto.setResult(Result.OK(s));
        }
    }
}
